factoryList = ["HD1","HD2","ZZ","DL","XY","HDPT","ZZPT"];
similarFactoryList = ["ZZ","XY","DL","HD1","HD2","CZ","WH"];
legendList = ["水", "燃气", "冷量", "压缩空气", "电", "   其他"];
workshopList = ["冲压车间", "总装车间", "焊装车间", "涂装车间", "树脂车间", "铸造车间"];

carbonRowTimeModulus = {
    "郑州工厂" : {
        2014 : {
            ele : 0.742,
            gas : 2.36
        },
        2015 : {
            ele : 0.742,
            gas : 2.36
        },
        2016 : {
            ele : 0.742,
            gas : 2.36
        },
        2017 : {
            ele : 0.734,
            gas : 2.36
        },
        2018 : {
            ele : 0.734,
            gas : 2.36
        },
        2019 : {
            ele : 0.5271,
            gas : 2.355
        }

    },
    "花都一工厂" : {
        2014 : {
            ele : 0.742,
            gas : 2.355
        },
        2015 : {
            ele : 0.742,
            gas : 2.355
        },
        2016 : {
            ele : 0.742,
            gas : 2.355
        },
        2017 : {
            ele : 0.734,
            gas : 2.355
        },
        2018 : {
            ele : 0.734,
            gas : 2.355
        },
        2019 : {
            ele : 0.5271,
            gas : 2.355
        }
    },
    "花都二工厂" : {
        2014 : {
            ele : 0.742,
            gas : 2.355
        },
        2015 : {
            ele : 0.742,
            gas : 2.355
        },
        2016 : {
            ele : 0.742,
            gas : 2.355
        },
        2017 : {
            ele : 0.734,
            gas : 2.355
        },
        2018 : {
            ele : 0.734,
            gas : 2.355
        },
        2019 : {
            ele : 0.5271,
            gas : 2.355
        }
    },
    "襄阳工厂" : {
        2014 : {
            ele : 0.742,
            gas : 2.355
        },
        2015 : {
            ele : 0.742,
            gas : 2.355
        },
        2016 : {
            ele : 0.742,
            gas : 2.355
        },
        2017 : {
            ele : 0.734,
            gas : 2.355
        },
        2018 : {
            ele : 0.734,
            gas : 2.355
        },
        2019 : {
            ele : 0.5271,
            gas : 2.355
        }
    },
    "大连工厂" : {
        2015 : {
            ele : 0.742,
            gas : 2.355
        },
        2016 : {
            ele : 0.742,
            gas : 2.355
        },
        2017 : {
            ele : 0.734,
            gas : 2.355
        },
        2018 : {
            ele : 0.734,
            gas : 2.355
        },
        2019 : {
            ele : 0.7769,
            gas : 2.355
        }
    }

};

costTimeModulus = {
    "郑州工厂" : {
        2014 : {
            ele : 0.61,
            gas : 3.28
        },
        2015 : {
            ele : 0.69,
            gas : 3.28
        },
        2016 : {
            ele : 0.66,
            gas : 3.54
        },
        2017 : {
            ele : 0.62,
            gas : 3.54
        },
        2018 : {
            ele : 0.54,
            gas : 3.67
        },
        2019 : {
            ele : 0.54,
            gas : 3.67
        }
    },
    "花都一工厂" : {
        2014 : {
            ele : 0.61,
            gas : 3.28
        },
        2015 : {
            ele : 0.69,
            gas : 3.28
        },
        2016 : {
            ele : 0.66,
            gas : 3.54
        },
        2017 : {
            ele : 0.62,
            gas : 3.54
        },
        2018 : {
            ele : 0.54,
            gas : 3.67
        },
        2019 : {
            ele : 0.51,
            gas : 3.68
        }
    },
    "大连工厂" : {
        2015 : {
            ele : 0.53,
            gas : 3.45
        },
        2016 : {
            ele : 0.48,
            gas : 2.83
        },
        2017 : {
            ele : 0.48,
            gas : 2.83
        },
        2018 : {
            ele : 0.45,
            gas : 2.88
        },
        2019 : {
            ele : 0.45,
            gas : 2.88
        }
    },
    "襄阳工厂" : {
        2014 : {
            ele : 0.64,
            gas : 3.75
        },
        2015 : {
            ele : 0.63,
            gas : 3.19
        },
        2016 : {
            ele : 0.63,
            gas : 2.47
        },
        2017 : {
            ele : 0.59,
            gas : 2.47
        },
        2018 : {
            ele : 0.59,
            gas : 2.59
        },
        2019 : {
            ele : 0.58,
            gas : 2.43
        }
    },
    "花都二工厂" : {
        2014 : {
            ele : 0.61,
            gas : 3.79
        },
        2015 : {
            ele : 0.71,
            gas : 3.79
        },
        2016 : {
            ele : 0.54,
            gas : 3.54
        },
        2017 : {
            ele : 0.58,
            gas : 3.46
        },
        2018 : {
            ele : 0.42,
            gas : 3.70
        },
        2019 : {
            ele : 0.48,
            gas : 3.68
        }
    }
};

frameworkIdToDevSn = {
    "郑州工厂" : {
        "yield" : 278,
        713 :{
            ele : 'E001',
            gas : 'G001'
        },
        1058 :{
            ele : 'E009'
        },
        1059 :{
            ele : 'E070'
        },
        1061 :{
            ele : 'E276'
        },
        1062 :{
            ele : 'E037',
            gas : 'G059'
        },
        1060 :{
            ele : 'E401',
            gas : 'G002'
        },
        719 :{
            ele : 'E001',
            gas : 'G001'
        },
        1063 :{
            ele : 'E009'
        },
        1064 :{
            ele : 'E070'
        },
        1066 :{
            ele : 'E276'
        },
        1067 :{
            ele : 'E037',
            gas : 'G059'
        },
        1065 :{
            ele : 'E401',
            gas : 'G002'
        }
    },
    "花都一工厂" : {
        "yield" : 11024,
        1081 :{
            ele : 'HDC001',
            gas : 'HDE001'
        },
        1365 :{
            ele : 'HDC002'
        },
        1405 :{
            ele : 'HDC003'
        },
        1485 :{
            ele : 'HDC005'
        },
        1445 :{
            ele : 'HDC004',
            gas : 'HDE004'
        },
        1525 :{
            ele : 'HDC007',
            gas : 'HDE007'
        },
        1565 :{
            ele : 'HDC006'
        },
        1082 :{
            ele : 'HDC001',
            gas : 'HDE001'
        },
        1366 :{
            ele : 'HDC002'
        },
        1406 :{
            ele : 'HDC003'
        },
        1486 :{
            ele : 'HDC005'
        },
        1446 :{
            ele : 'HDC004',
            gas : 'HDE004'
        },
        1526 :{
            ele : 'HDC007',
            gas : 'HDE007'
        },
        1566 :{
            ele : 'HDC006'
        }
    }
};

/**
 * 获取产量id
 * @param factoryName
 * @returns {number}
 */
function getFactoryYieldId (factoryName) {
    if (!factoryName){
        return -1;
    }
    return frameworkIdToDevSn[factoryName] ? frameworkIdToDevSn[factoryName].yield : -1;
}


/**
 * 获取产量
 * @param yieldId
 * @param time
 * @returns
 */
function getFactoryYield (yieldId, time) {
    if (!yieldId && !time){
        return;
    }
    var db = iot.db();
    var yieldResult = db.queryOne("select SUM(dvalue) totalSum from iot_dev_data_win" +
        " where dev_id = "+ yieldId +" and the_time like '"+ time +"%'");
    return yieldResult;
}

/**
 * 获取工厂子级占比参数
 * @param factoryName
 * @param energyName
 */
function getFactoryParam (factoryName, energyName) {
    if (!factoryName){
        return;
    }

    var db = iot.db();
    var carbonRowToEleAndGas = db.qry("SELECT * FROM `energy_struc` where type = '"+ energyName +"' and `level` <= 1\n" +
        "UNION all\n" +
        "select * from `energy_struc` where type = '电' and `level` <= 1\n" +
        "union all\n" +
        "select * from `energy_struc` where type = '燃气' and `level` <= 1;");
    if (carbonRowToEleAndGas){
        var result = {}
        var resultItem = {
            ele : 0,
            gas : 0
        }
        var id;
        carbonRowToEleAndGas.forAll(function (item) {
            if (factoryName === item.name && item.type === energyName){
                id = item.id
                result[item.id] = resultItem
            }
            if (factoryName === item.name && item.type === "电"){
                resultItem.ele = item.id
            }
            if (factoryName === item.name && item.type === "燃气"){
                resultItem.gas = item.id
            }
        });
        return result;
    }
}

/**
 * 根据时间获取上一年的时间
 * @param time
 */
function getPreYearTime(time) {
    var preYear = time.substring(0, 4) - 1;
    return preYear + time.substring(4, time.length);
}

/**
 * 通过时间获取年份加后面的月份，天
 * @param year
 * @param time
 */
function getTime(year, time) {
    if (time.length() > 4) {
        return year + time.substring(4, time.length());
    }
    return year;
}

/**
 * 根据time的格式获取当前时间
 * @param time
 */
function getCurrentTimeByType(timeType) {
    var date = new Date;
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    month = (month < 10 ? "0" + month : month);
    day = (day < 10 ? "0" + day : day);
    hour = (hour < 10 ? "0" + hour : hour);

    var typeTime = {
        'year': month,
        'month': day,
        'day': hour
    };
    var currTime = {
        'year': year,
        'month': year + "-" + month,
        'day': year + "-" + month + "-" + day
    };
    var result = {
        typeTime : typeTime[timeType],
        currTime : currTime[timeType]
    }
    return result;
}

/**
 * 返回近4年数组
 * @param time
 */
function getNearlyFourYears(time) {
    var result = iot.list();
    var year = time.substring(0, 4);
    for (var index = 3; index >= 0; index--){
        result.add(year - index + '');
    }
    return result;
}

/**
 * 根据时间返回对应时间数组
 * @param time
 */
function getTimeList(time) {
    var timeObj = dtime.parseStringToDateTimeViaPattern(time);
    var times = 0;
    switch (time.length) {
        case 4:
            times=timeObj.monthOfYear().getMaximumValue();
            break;
        case 7:
            times=timeObj.dayOfMonth().getMaximumValue();
            break;
        case 10:
            times=timeObj.hourOfDay().getMaximumValue() + 1;
            break;
        case 13:
            times=timeObj.minuteOfHour().getMaximumValue() + 1;
            break;
        default:
            break;
    }
    var result = iot.list();
    var start = 1;
    var end = times;
    if (time.length >= 10){
        start = 0;
        end = times - 1;
    }
    for (var index = start; index <= end; index++){
        if (index < 10){
            index = "0" + index;
        }
        result.add(index + "");
    }

    return result;
}

/**
 * 通过工厂名称获取工厂+工厂车间的名称
 * @param factory
 * @param workSpaceName
 * @returns {*}
 */
function getFactoryWorkShop(factory, workSpaceName) {
    var index = workSpaceName.indexOf("/");
    if (index > -1) {
        return factory + workSpaceName.substring(index, workSpaceName.length());
    }
    return factory;
}

/**
 * 对数据的小数点保留规则判断
 * @param number
 */
function decimalRetainRule (number) {
    number = parseFloat(number)
    if (!number && number === 0) {
        return 0;
    }
    var numberFormate = iot.getNumberFormat("0.0");
    if (number > 100) {
        return parseInt(number);
    } else {
        return numberFormate.format(number);
    }
}

/**
 * 获取工厂名称
 * @param workSpaceName
 */

function getFactoryName (workSpaceName) {
    var arr = workSpaceName.split("/");
    return arr.length > 1 ? arr[1] : "PV";
}

/**
 * 计算环比
 * @param nowTimeData
 * @param lastTimeData
 * @returns {string}
 */
function calculateRatio (nowTimeData, lastTimeData) {
    var ratioResult = '+0%';

    var ratioTotalSum = nowTimeData - lastTimeData;
    if(lastTimeData && lastTimeData !== 0){
        var ratioValue = ratioTotalSum / lastTimeData;
        if(ratioTotalSum >= 0){
            ratioResult = "+"+iot.getPercentage(ratioValue);
        }else{
            ratioResult = iot.getPercentage(ratioValue);
        }
    }
    return ratioResult;
}



